//
//  HUSelectPlateNumber.m
//  选择车牌
//
//  Created by Mac on 16/1/4.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "HUSelectPlateNumber.h"



//而且，这个分类创建的蛮有意思
@implementation UIButton (pickButton)
- (void)defineMyButton:(NSString *)string {
    self.bounds = CGRectMake(0, 0, 80, 30);
//    self.backgroundColor = [UIColor redColor];
    self.titleEdgeInsets = UIEdgeInsetsMake(4, 4, 4, 4);
    [self setTitle:string forState:UIControlStateNormal];
    
//    [self setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}

@end


@interface HUSelectPlateNumber ()

{
    UILabel* plateLabel;
}

@end


@implementation HUSelectPlateNumber 

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self initData];
    [self show];
    [self addTap];
    
    
    
    UIView* headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.width / 10)];
//    headView.backgroundColor = [UIColor colorWithRed:208 / 255. green:69 / 255. blue:28 / 255. alpha:1];
    headView.backgroundColor = [UIColor whiteColor];
    
    //用center 和 bounds来创建控件 蛮好用
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.center = CGPointMake(40, headView.frame.size.height / 2);
    [cancelButton defineMyButton:@"取消"];
    [cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton* confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmButton.center = CGPointMake(headView.frame.size.width - 40, headView.frame.size.height / 2);
    [confirmButton defineMyButton:@"确定"];
    [confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    
    plateLabel = [[UILabel alloc]init];
    plateLabel.bounds = CGRectMake(0, 0, 120, headView.frame.size.height );
    plateLabel.center = headView.center;
    plateLabel.textAlignment = 1;
    plateLabel.textColor = [UIColor blackColor];
    plateLabel.text = self.selectPlate;

    [headView addSubview:cancelButton];
    [headView addSubview:confirmButton];

    [self addSubview:self.platePicker];
    [self addSubview:headView];
    [self addSubview:plateLabel];
    
    
    
    
}

- (void)initData {
    //获取plist文件path
    NSString* string = [[NSBundle mainBundle]pathForResource:@"plateArray" ofType:@"plist"];
    
    //读取plist文件内容
    NSArray* array = [[NSArray array]initWithContentsOfFile:string];
    
    //
    self.plateArray = [array mutableCopy];
    
    //设置一个默认的车牌号
    self.selectPlate = [NSMutableString stringWithString:@"京A00000"];
}


//实例化
+ (instancetype)showOnView:(UIView *)view
{
//    HUSelectPlateNumber *plateNumber = [[HUSelectPlateNumber alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width  , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width / 5 * 4)];
    
    
    HUSelectPlateNumber *plateNumber = [[HUSelectPlateNumber alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width  , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    plateNumber.mask = [[UIView alloc] initWithFrame:view.bounds];
    plateNumber.mask.backgroundColor = [UIColor blackColor];
    plateNumber.mask.alpha = 0.3;
    [view addSubview:plateNumber.mask];
    [view addSubview:plateNumber];
    return plateNumber;
}

#pragma mark 显示\隐藏动画
- (void)show
{
#warning CGAffineTransformTranslate 这是什么鬼
    self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^(void) {
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL isFinished) {
    }];
}

- (void)hide
{
    [UIView animateWithDuration:0.5 animations:^(void) {
        self.transform = CGAffineTransformTranslate(self.transform, 0, self.frame.size.height);
        self.mask.alpha = 0;
    } completion:^(BOOL isFinished) {
        [self.mask removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)addTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self.mask addGestureRecognizer:tap];
}

#pragma mark 点击事件代理方法
- (void)cancel {
    self.plateBlock(nil);
    [self hide];
}

- (void)confirm {
//    [self.delegate confirmSelect:self.year month:self.month];
    if (self.plateBlock) {
        self.plateBlock(self.selectPlate);
    }
    [self hide];
}
#pragma mark pickerDelegate DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    //一共有7个组件
    return self.plateArray.count;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //每个组件有多少个内容
    NSArray* rowArray = [self.plateArray objectAtIndex:component];
    return rowArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSArray* rowArray = [self.plateArray objectAtIndex:component];
    NSString* title = [rowArray objectAtIndex:row] ;
    
    return title;
}

#pragma mark - 改变车牌号
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    for (NSInteger i = 0 ; i < 7; i++) {
        NSInteger list = [pickerView selectedRowInComponent:i];
        NSArray* rowArray = [self.plateArray objectAtIndex:i];
        NSString* string = [rowArray objectAtIndex:list];
        [self.selectPlate replaceCharactersInRange:NSMakeRange(i, 1) withString:string];
    }
    plateLabel.text = self.selectPlate;
}

#pragma  mark Set && get
- (UIPickerView *)platePicker {
    if (!_platePicker) {
        _platePicker = [[UIPickerView alloc]init];
//        _platePicker.center = CGPointMake(self.center.x, self.frame.size.width * 0.4 + 20);
//        _platePicker.bounds = CGRectMake(0, 0, self.frame.size.width , self.frame.size.width / 5 * 4);
        
        

        _platePicker.frame = self.bounds;
        _platePicker.delegate = self;
        _platePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _platePicker;
}

@end

